package com.totoro.web;

import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.totoro.domain.Actor;
import com.totoro.domain.UserRole;
import com.totoro.service.ActorService;
import com.totoro.service.UserService;

@Controller
@RequestMapping("/")
public class LoginController {

	@Autowired
	private UserService userService;

	@Autowired
	private ActorService actorService;

	@RequestMapping("/accessDenied")
	public String list() {
		return "/accessDenied";
	}

	@RequestMapping("/index")
	public ModelAndView index(Authentication authentication) {
		// 用户名称
		String username = authentication.getName();
		System.out.println("用户名：" + username);

		Collection<UserRole> userRoleList = userService.uniqueResult(
				"username", username).getUserRole();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/index");
		mv.addObject("userRoleList", userRoleList);
		return mv;
	}

	@RequestMapping("/index1")
	public String index() {
		return "/index1";
	}

	@RequestMapping("/admin_top")
	public ModelAndView admin_top(Authentication authentication) {
		String username = authentication.getName();
		Actor actor = actorService.uniqueResult("no", username);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/admin_top");
		mv.addObject("actor", actor);
		return mv;
	}

	@RequestMapping("/left")
	public ModelAndView left(Authentication authentication) {
		// 用户名称
		String username = authentication.getName();
		System.out.println("用户名：" + username);
		// 用户权限关联对象集合
		Collection<UserRole> userRoleList = userService.uniqueResult(
				"username", username).getUserRole();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/left");
		mv.addObject("userRoleList", userRoleList);
		return mv;
	}
}
